package lch.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import lch.entity.PageResult;
import lch.entity.QueryPageBean;
import lch.entity.Result;

import lch.pojo.Menu;
import lch.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lch
 * @date 2020-01-21 13:40
 */
@RequestMapping("/menu")
@RestController
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {

        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "新增菜单数据失败");
        }
        return new Result(true, "新增菜单数据成功");
    }


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = menuService.pageQuery(queryPageBean);

        return pageResult;
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            menuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "删除菜单数据失败");
        }
        return new Result(true, "删除菜单数据成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, "菜单数据回显成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "菜单数据回显失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
            return new Result(true, "编辑菜单数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "编辑菜单数据失败");
        }
    }

}
